package com.doctor.web.controller;

import com.doctor.common.core.controller.BaseController;
import com.doctor.common.core.page.TableDataInfo;
import com.doctor.common.utils.DateUtils;
import com.doctor.common.utils.SecurityUtils;
import com.doctor.web.domain.OrderDO;
import com.doctor.web.domain.ProductDO;
import com.doctor.web.service.OrderService;
import com.doctor.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.ServletSecurity;
import java.util.Date;
import java.util.List;

/**
 * 订单
 *
 * @since 2025-04-11
 */
@RestController
@RequestMapping("/maincode/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 添加订单 order_status=0
     * @return 操作结果
     */
    @PostMapping("/add")
    public AjaxResult addOrder(@RequestParam Long productId) {
        Long id = orderService.save(productId);
        return AjaxResult.success("订单添加成功",id);
    }

    /**
     * 支付订单 order_status=1
     */
    @PostMapping("/pay")
    public AjaxResult payOrder(@RequestBody OrderDO orderDO) {
        orderService.payOrder(orderDO);
        return AjaxResult.success("订单添加成功");
    }


    /**
     * 删除订单
     * @param id 订单 ID
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('maincode:order:delete')")
    @PostMapping("/delete/{id}")
    public AjaxResult deleteOrder(@PathVariable Long id) {
        boolean result = orderService.removeById(id);
        if (result) {
            return AjaxResult.success("订单删除成功");
        } else {
            return AjaxResult.error("订单删除失败");
        }
    }

    /**
     * 修改订单
     * @param orderDO 订单实体
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('maincode:order:update')")
    @PostMapping("/update")
    public AjaxResult updateOrder(@RequestBody OrderDO orderDO) {
        orderDO.setUpdateTime(DateUtils.getNowDate());
        boolean result = orderService.updateById(orderDO);
        if (result) {
            return AjaxResult.success("订单修改成功");
        } else {
            return AjaxResult.error("订单修改失败");
        }
    }

    /**
     * 查询单个订单
     * @param id 订单 ID
     * @return 订单实体
     */
    @PreAuthorize("@ss.hasPermi('maincode:order:get')")
    @GetMapping("/{id}")
    public AjaxResult getOrder(@PathVariable Long id) {
        OrderDO orderDO = orderService.getById(id);
        if (orderDO != null) {
            return AjaxResult.success("订单查询成功", orderDO);
        } else {
            return AjaxResult.error("未找到该订单");
        }
    }

    /**
     * 查询所有订单
     * @return 订单列表
     */
    @GetMapping("/list")
    public TableDataInfo getAllOrders() {
        startPage();
        List<OrderDO> list = orderService.list();
        return getDataTable(list);
    }

    /**
     * 查询当前用户所有订单
     * @return 订单列表
     */
    @GetMapping("/list/my")
    public TableDataInfo getAllOrdersMy() {
        // 获取当前用户的 userId
        Long userId = SecurityUtils.getUserId();
        startPage();
        // 调用服务层方法查询用户订单
        List<OrderDO> list = orderService.getOrdersByUserId(userId);
        return getDataTable(list);
    }


    /**
     * 根据订单状态查询当前用户所有订单
     * @param orderStatus 订单状态
     * @return 订单列表
     */
    @GetMapping("/list/my/byStatus")
    public TableDataInfo getOrdersByStatusAndUser(@RequestParam Integer orderStatus) {
        // 获取当前用户的 userId
        Long userId = SecurityUtils.getUserId();
        startPage();
        // 调用服务层方法查询用户订单
        List<OrderDO> list = orderService.getOrdersByUserIdAndStatus(userId, orderStatus);
        return getDataTable(list);
    }

    /**
     * 根据订单 ID 修改订单状态
     * @param orderId 订单 ID
     * @param orderStatus 订单状态
     * @return 操作结果
     */
    @PostMapping("/updateStatus")
    public AjaxResult updateOrderStatus(@RequestParam Long orderId, @RequestParam Integer orderStatus) {
        boolean result = orderService.updateOrderStatus(orderId, orderStatus);
        if (result) {
            return AjaxResult.success("订单状态修改成功");
        } else {
            return AjaxResult.error("订单状态修改失败");
        }
    }


}